<?php
	require_once 'environment.php';
	
	if(!$acc->isLogged()) $temp->addSubMenu("Become a Sponsor", "sponsors.php?action=join");
	if($acc->isLogged()) {
		$temp->addSubMenu("Add Sponsor", "sponsors.php?action=add");
		$temp->addSubMenu("Edit Sponsor", "sponsors.php?action=edit");
		$temp->addSubMenu("Delete Sponsor", "sponsors.php?action=del");
	}
	
	$_sponsors = new Sponsor($acc, $db);
	
	function throwError($where, $what) {
		print change_site("sponsors.php?action={$where}&error={$what}");
		die();
	}
	
	switch(fixArr($_GET, "action")) {
		default:
			$temp->setTab(4);
			$temp->currentPage("Home >> Sponsors");
			$temp->generate();
			$_sponsors->listSponsors();
			break;
		case("join"):
			$temp->setTab(4, 0);
			$temp->currentPage("Home >> Sponsors >> Become a Sponsor");
			$temp->generate();
            if(!fixArr($_POST, "name")){
                print "<p>So, you wanna become a sponsor? Fill out the fields below and leave a comment and we'll get back to you as soon as possible!</p>";
                print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>\n";
                print "<form method=\"POST\" action=\"\">\n";
                print "<table style=\"margin-left: 0px;\">\n";
                print "<tr>\n<td><label>Name:</td>\n<td><input type=\"text\" name=\"name\" /></label></td>\n</tr>\n";
                print "<tr>\n<td><label>Email:</td>\n<td><input type=\"text\" name=\"email\" /></label></td>\n</tr>";
                print "<tr>\n<td><label>Phone Number:</td>\n<td><input type=\"text\" name=\"phone\" /></label></td>\n</tr>";
                print "<tr>\n<td style=\"vertical-align:text-top;\"><label>Comments:</td>\n<td><textarea style=\"resize:none;\" rows=\"5\" cols =\"40\" name=\"comments\"></textarea></td>\n</tr>\n";
                print "</table>\n";
                print "<input type=\"submit\" value=\"Send\" /> <input type=\"reset\" value=\"Reset\" />\n";
                print "</form>\n";
            }else{
                $name = fixArr($_POST, "name");
                $email = fixArr($_POST, "email");
                $phone = fixArr($_POST, "phone");
                $comments = fixArr($_POST, "comments");
                mail("shane.king.2008@gmail.com, monsterministries@cableone.net", "New Sponsor Application!", "Name: {$name}\n\nEmail: {$email}\n\nPhone Number: {$phone}\n\nComments:\n{$comments}", "From: Sponsor Form <sponsor@revelationmonstertruck.org>");
                print "Your message has been successfully sent!";
            }
			break;
		case("add"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(4, 0);
			$temp->currentPage("Home >> Sponsors >> Add Sponsor");
			$temp->generate();
			if(!fixArr($_POST, "sName")){
				print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error") : "")."</span>\n";
				print "<p>The sponsor logo should be linked to the same way images are when you add them. Use a site like <a href=\"http://imageshack.us\">ImageShack.us</a> to upload the logo and copy the DIRECT link for the logo and paste it into the \"Sponsor Logo\" box.</p>";
				print "<form action=\"\" method=\"POST\">\n";
				print "<label>Sponsor Name: <input type=\"text\" name=\"sName\" /></label><br />\n";
				print "<label>Sponsor Website: <input type=\"text\" name=\"URL\" /></label><br />\n";
				print "<label>Sponsor Logo: <input type=\"text\" name=\"logo\" /></label><br />\n";
				print "<label>Description:<br />\n<textarea style=\"resize:none;\" name=\"desc\" rows=\"5\" cols =\"40\"></textarea></label><br />\n";
				print "<input type=\"submit\" value=\"Add Sponsor\" /> <input type=\"reset\" value=\"Reset\" />\n";
				print "</form>\n";
			}else{
				$name = fixArr($_POST, "sName");
				$site = fixArr($_POST, "URL");
				$desc = fixArr($_POST, "desc");
				$logo = fixArr($_POST, "logo");
				$return = $_sponsors->addSponsor($name, $desc, $site, $logo);
				if(is_array($return)){
					throwError("add", $return[1]);
				}else{
					print "You have successfully added a sponsor!";
				}
			}
			break;
		case("edit"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(4, 1);
			$temp->currentPage("Home >> Sponsors >> Edit Sponsor");
			$temp->generate();
			if(!fixArr($_GET, "id")) {
				$sql = $db->Execute("SELECT * FROM `monster`.`sponsors`");
				if($sql->RecordCount() > 0) {
					print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>\n";
					print "<b>Edit a sponsor</b>:";
					while($row = $sql->FetchRow()) {
						print "<br />\n<a href=\"sponsors.php?action=edit&id={$row['AutoInc']}\">{$row['Sponsor Name']}</a><br />\n";
					}
				}else{
					print "There are currently no sponsors.";
				}
			}else{
                $id = fixArr($_GET, "id");
				if(!fixArr($_POST, "sName")) {
					$data = $_sponsors->getSponsorByID($id);
					if(!$data) throwError("edit", "A sponsor with this ID does not exist!");
					print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error") : "")."</span>\n";
					print "<p>The sponsor logo should be linked to the same way images are when you add them. Use a site like <a href=\"http://imageshack.us\">ImageShack.us</a> to upload the logo and copy the DIRECT link for the logo and paste it into the \"Sponsor Logo\" box.</p>";
					print "<form action=\"\" method=\"POST\">\n";
					print "<label>Sponsor Name: <input type=\"text\" name=\"sName\" value=\"{$data['Sponsor Name']}\" /></label><br />\n";
					print "<label>Sponsor Website: <input type=\"text\" name=\"URL\" value=\"{$data['Sponsor Link']}\" /></label><br />\n";
					print "<label>Sponsor Logo: <input type=\"text\" name=\"logo\" value=\"{$data['Sponsor Logo']}\" /></label><br />\n";
					print "<label>Description:<br />\n<textarea style=\"resize:none;\" name=\"desc\" rows=\"5\" cols =\"40\">{$data['Sponsor Description']}</textarea></label><br />\n";
					print "<input type=\"submit\" value=\"Change Sponsor\" /> <input type=\"reset\" value=\"Reset\" />\n";
					print "</form>\n";
				}else{
					$name = fixArr($_POST, "sName");
					$site = fixArr($_POST, "URL");
					$desc = fixArr($_POST, "desc");
					$logo = fixArr($_POST, "logo");
					$return = $_sponsors->editSponsor($id, $name, $desc, $site, $logo);
					if(is_array($return)) {
						throwError("edit&id={$id}", $return[1]);
					}else{
						print "You have successfully edited the sponsor!";
					}
				}
			}
			break;
		case("del"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(4, 2);
			$temp->currentPage("Home >> Sponsors >> Delete Sponsor");
			$temp->generate();
            if(!fixArr($_GET, "id")){
                $sql = $db->Execute("SELECT * FROM `monster`.`sponsors`");
                if($sql->RecordCount() > 0){
                    while($row = $sql->FetchRow()){
                        print "<br />\n{$row['Sponsor Name']} - <a href=\"sponsors.php?action=del&id={$row['AutoInc']}\">Delete</a><br />\n";
                    }
                }else{
                    print "I'm sorry, but there are currently no sponsors in the database.";
                }
            }else{
                $id = fixArr($_GET, "id");
                if(fixArr($_GET, "conf") !== "true"){
                    $info = $_sponsors->getSponsorByID($id);
                    print "Are you SURE you want to remove this sponsor? ({$info['Sponsor Name']}) This can <b>NOT</b> be undone!<br />\n<br />\n";
                    print "<a href=\"sponsors.php?action=del\">No, I do not want to delete this sponsor.</a><br />\n<br />\n";
                    print "<a href=\"sponsors.php?action=del&id={$id}&conf=true\">Yes, remove this sponsor permanently.</a><br />\n<br />\n";
                }else{
                    if($_sponsors->removeSponsor($id)){
                        print "You have successfully removed the sponsor.";
                    }else{
                        print "A sponsor with this ID does not exist.";
                    }
                }
            }
			break;
	}
?>